<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page language="java" import="util.*, java.sql.*, beans.*" %>
<html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
		<link type="text/css" href="1.css" rel="stylesheet" media="screen" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Change Quantity Category</title>
    </head>
    <body>	
        <p>&nbsp;</p>
        <p align="right">
            <UI>
            <%
            String msg = util.Utilities.isCorrectStaffLogin(session);
            String name = (String)session.getAttribute("username");
            if(msg != null) {
                out.println(msg);
            }else {
            %>
        <B>Hi!<%= name%></B>        </p>
        <p align="right"><a href="logout.jsp">Click here to logout</a></p>
        <center><h1>Change Quantity Page</h1></center>
        <tr>
            <td height="31" colspan="2"><div>
                    <SCRIPT src="outofstock.js"></SCRIPT>
            </div>	</td>
        </tr>
        <table align="center" border="1">
		<td id="content">
            <tr bgcolor="white"><b>
                    <td>Item ID:</td>
                    <td>Name</td>
                    <td>Type</td>
                    <td>Company</td>
                    <td>Scale</td>
                    <td>Price</td>
                    <td>Description</td>
                    <td>Image</td>
                    <td>Quantity</td>
                    <td>Change Quantity</td>
                </b>
            </tr>
			</td>
            <%
            final String sPage = (String)request.getParameter("page");
            int page1 = 0; //<====
            
            
            final int recordPerPage = 5;
            final int indexPerPage =10;
            final int count = ModelBean.getTotalRecordCount(); //<=======
            final int indexCount = (int)Math.floor(count / (double)recordPerPage);
            if(page1 > indexCount) {
                page1 = 0;
            }
            if(util.Utilities.isValid(sPage)) {
                try {
                    page1 = Integer.parseInt(sPage);
                } catch(Exception e) {
                    
                }
            }
            final int k=page1;
            
            %>
            <%
            try {
                
                ModelBean[] models = ModelBean.getAllModel();
                
                for(int z=recordPerPage*page1; z<(page1+1)*recordPerPage; z++ ) {
                    
                    String itemId = models[z].getItemID();
                    String name1 = models[z].getName();
                    String type = models[z].getType();
                    String company = models[z].getCompany();
                    String scale = models[z].getScale();
                    String price = models[z].getPrice();
                    String description = models[z].getDescription();
                    String imageURL = models[z].getImageURL();
                    int quantity1 = models[z].getQuantity();
            %>
            <form action='changed.jsp' method='get'>
                <tr>
                <td>
                    <%=itemId%>
                    <input type='hidden' name='itemID' value='<%=itemId%>' />
                </td>
                <td>
                    <%=name1%>
                    <input type='hidden' name='name' value='<%=name1%>' />
                </td>
                <td>
                    <%=type%>
                    <input type='hidden' name='type' value='<%=type%>' />
                </td>
                <td>
                    <%=company%>
                    <input type='hidden' name='company' value='<%=company%>' />
                </td>
                <td>
                    <%=scale%>
                    <input type='hidden' name='scale' value='<%=scale%>' />
                </td>
                <td>
                    $<%=price%>
                    <input type='hidden' name='price' value='<%=price%>' />
                </td>
                <td>
                    <%=description%>
                    <input type='hidden' name='description' value='<%=description%>' />
                </td>
                <td>
                    <a href='../photo.jsp?imageURL=<%=imageURL%>'><img src='../../image/<%=imageURL%>' width=150 height=200/></a>
                    <input type='hidden' name='imageURL' value='<%=imageURL%>' />
                </td>
                <td>
                    <input type='TEXT' size='5' name='quantity1' value='<%=quantity1%>'>                        
                </td>
                <td valign='top'>
                <input type='Submit' value='Change'>
                </td>
            </form>
			
            <%
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            %>
			    </tr>
        </table>
            <%!                                
            void printIndex(int k, int i, JspWriter out1) {
                try {
                    if (i==k) {
                        out1.println("<b><a href='changeQuantity.jsp?page="+i+"'>"+(i+1)+"</a></b>");
                    }else {
                        out1.println("<a href='changeQuantity.jsp?page="+i+"'>"+(i+1)+"</a>");
                    }
                }catch(Exception e) {
                    
                }
            }
            
            %>
            <form method='get' action='changeQuantity.jsp'>
                <center>
			    <td height="44" colspan="3" id="footer"><h1>
                    <%
                    int nextValue = 5;
                    int remain = count-(indexCount+page1);
                    if (remain < indexPerPage) {
                    nextValue = remain % indexPerPage;
                    }
                    if(k < 5) {
                    for(int i=0; i<=indexCount&&i<indexPerPage; i++) {
                    printIndex(k, i, out);
                    }
                    }else if(k >= indexCount-indexCount%indexPerPage -1) {
                    int start = indexCount - indexPerPage;
                    for(int y=0; y <indexPerPage; y++) {
                    printIndex(k, y+start, out);
                    }
                    }else {
                    for (int i=0; i<5; i++) {
                    printIndex(k, page1-5+i, out);
                    }
                    for (int i=0; i< 5&& page1+i<indexCount; i++) {
                    printIndex(k, page1+i, out);
                    }
                    }
                    
                    %>
                    <input type='hidden' name='page' value='<%=k+1%>' />
                    <input type = submit value='Next Page'/>
					</h1>
					</td>
                </center>
            </form>
        
        <%
            } %>
    </body>
</html>